#include "cJSON.h"
#include "stdio.h"
#include "stdlib.h"

void test1() {
    cJSON* root = cJSON_CreateObject();
    cJSON* key1 = cJSON_AddNumberToObject(root, "id", 1);
    if (key1) {
        printf("key1: %d\n", key1->valueint);
    }
    cJSON_AddItemToObject(root, "name", cJSON_CreateString("John"));
    cJSON_AddItemToObject(root, "name", cJSON_CreateString("John"));

    // 打印JSON对象
    char* str = cJSON_Print(root);
    printf("%s\n", str);
}

char* read_json_file2(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (!file) {
        fprintf(stderr, "无法打开文件: %s\n", filename);
        return NULL;
    }

    // 获取文件大小
    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);

    // 分配内存
    char* buffer = (char*)malloc(length + 1);
    if (!buffer) {
        fclose(file);
        fprintf(stderr, "内存分配失败\n");
        return NULL;
    }

    // 读取文件内容
    fread(buffer, 1, length, file);
    buffer[length] = '\0';  // 添加字符串结束符

    fclose(file);
    return buffer;
}

void test2() {
    char* file_str = "lib/1.json";
    FILE* file = fopen(file_str, "r");
    if (!file) {
        perror("fopen failed\n");
        return;
    }
    char buffer[1024];
    while (fgets(buffer, sizeof(buffer), file)) {
        printf("%s", buffer);
    }
    fclose(file);
}

void test3() {
    char* file_str = "lib/1.json";
    char* json_str = read_json_file2(file_str);
    if (!json_str) {
        perror("read_json_file failed\n");
        return;
    }
    cJSON* root = cJSON_Parse(json_str);
    if (!root) {
        printf("cJSON_Parse failed\n");
        return;
    }
    char* str = cJSON_Print(root);
    if (!str) {
        printf("cJSON_Print failed\n");
        return;
    }
    printf("%s\n", str);
    free(str);
}

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

#include "cJSON.h"

// 配置结构体
typedef struct {
    double default_food_last_time;
    int default_food_weight;
    int default_food_pro_weight;
    int default_snake_length;
    int default_food_pro_probability;
    bool flexible_snake;
    bool strong_snake;
    bool immortal_snake;
} GameConfig;

// 检查文件是否存在并可读
int is_file_valid(const char* filename) {
    struct stat st;

    // 检查文件是否存在
    if (stat(filename, &st) != 0) {
        perror("stat failed");
        fprintf(stderr, "no file: %s\n", filename);
        return 0;
    }

    // 检查是否是普通文件
    if (!S_ISREG(st.st_mode)) {
        fprintf(stderr, "is not normal file: %s\n", filename);
        return 0;
    }

    // 检查是否有读取权限
    if ((st.st_mode & S_IRUSR) == 0) {
        fprintf(stderr, "no read or write permission: %s\n", filename);
        return 0;
    }

    return 1;
}

// 从文件读取JSON内容
char* read_json_file(const char* filename) {
    FILE* file = fopen(filename, "r");
    if (!file) {
        fprintf(stderr, "can't open file: %s\n", filename);
        perror("fopen error");
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    long length = ftell(file);
    fseek(file, 0, SEEK_SET);

    char* buffer = (char*)malloc(length + 1);
    if (!buffer) {
        fclose(file);
        fprintf(stderr, "malloc failed\n");
        return NULL;
    }

    fread(buffer, 1, length, file);
    buffer[length] = '\0';

    fclose(file);
    return buffer;
}

// 解析JSON到结构体
int parse_config(const char* json_string, GameConfig* config) {
    cJSON* json = cJSON_Parse(json_string);
    if (!json) {
        const char* error_ptr = cJSON_GetErrorPtr();
        if (error_ptr) {
            fprintf(stderr, "JSON parse error: %s\n", error_ptr);
        }
        return 0;
    }

    // 解析各个字段
    cJSON* item = NULL;

    item = cJSON_GetObjectItemCaseSensitive(json, "DEFAULT_FOOD_LAST_TIME");
    if (cJSON_IsNumber(item)) {
        config->default_food_last_time = item->valuedouble;
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "DEFAULT_FOOD_WEIGHT");
    if (cJSON_IsNumber(item)) {
        config->default_food_weight = item->valueint;
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "DEFAULT_FOOD_PRO_WEIGHT");
    if (cJSON_IsNumber(item)) {
        config->default_food_pro_weight = item->valueint;
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "DEFAULT_SNAKE_LENGTH");
    if (cJSON_IsNumber(item)) {
        config->default_snake_length = item->valueint;
    }

    item =
        cJSON_GetObjectItemCaseSensitive(json, "DEFAULT_FOOD_PRO_PROBABILITY");
    if (cJSON_IsNumber(item)) {
        config->default_food_pro_probability = item->valueint;
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "FLEXIBLE_SNAKE");
    if (cJSON_IsBool(item)) {
        config->flexible_snake = cJSON_IsTrue(item);
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "STRONG_SNAKE");
    if (cJSON_IsBool(item)) {
        config->strong_snake = cJSON_IsTrue(item);
    }

    item = cJSON_GetObjectItemCaseSensitive(json, "IMMORTAL_SNAKE");
    if (cJSON_IsBool(item)) {
        config->immortal_snake = cJSON_IsTrue(item);
    }

    cJSON_Delete(json);
    return 1;
}
#include <unistd.h>

// 打印配置信息
void print_config(const GameConfig* config) {
    printf("Game Configuration:\n");
    printf("DEFAULT_FOOD_LAST_TIME: %.1f\n", config->default_food_last_time);
    printf("DEFAULT_FOOD_WEIGHT: %d\n", config->default_food_weight);
    printf("DEFAULT_FOOD_PRO_WEIGHT: %d\n", config->default_food_pro_weight);
    printf("DEFAULT_SNAKE_LENGTH: %d\n", config->default_snake_length);
    printf("DEFAULT_FOOD_PRO_PROBABILITY: %d\n",
           config->default_food_pro_probability);
    printf("FLEXIBLE_SNAKE: %s\n", config->flexible_snake ? "true" : "false");
    printf("STRONG_SNAKE: %s\n", config->strong_snake ? "true" : "false");
    printf("IMMORTAL_SNAKE: %s\n", config->immortal_snake ? "true" : "false");
}

int test4() {
    char buffer[1024];
    if (getcwd(buffer, sizeof(buffer)) != NULL) {
        printf("Current working directory: %s\n", buffer);
    } else {
        perror("getcwd() error");
    }

    is_file_valid("D:/Code/vsCode/about_json/lib/config.json");
    // 读取JSON文件
    char* json_string = read_json_file("./lib/config.json");
    if (!json_string) {
        return 1;
    }

    // 解析配置
    GameConfig config;
    if (!parse_config(json_string, &config)) {
        free(json_string);
        return 1;
    }

    // 打印配置
    print_config(&config);

    free(json_string);
}

int main() {
    // test1();
    // test2();
    // test3();
    test4();
    return 0;
}